package com.hmkj.quote.job;

import com.hmkj.core.api.ufx.call.QuotationCall;
import com.hmkj.core.constant.CacheID;
import com.hmkj.core.constant.RiskNID;
import com.hmkj.core.model.quote.KlineData;
import com.hmkj.core.model.quote.TrendData;
import com.hmkj.core.po.commo.Commo;
import com.hmkj.core.service.cache.RedisService;
import com.hmkj.core.service.risk.RiskParamService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 行情数据定时器
 *
 * @author Vector
 * @create 2017-06-01 16:22
 */
@Slf4j
@Component
@Lazy(value = false)
public class QuotationJob {
    @Resource
    private RedisService redisService;
    @Resource
    private RiskParamService riskParamService;

    /**
     * 获取行情数据定时器，分时、K线  每分钟执行
     */
    @Scheduled(cron = "0 * * * * ?")
    public void quoteJob() throws InterruptedException {
        List<Commo> list = redisService.getList(CacheID.COMMO_LIST, Commo.class);
        if (CollectionUtils.isNotEmpty(list)) {
            for (Commo commo : list) {
                try {
                    String exchType = riskParamService.getParamValue(RiskNID.EXCH_TYPE, commo.getId());
                    String contract = riskParamService.getContractNo(commo.getId());

                    if (commo.getType().intValue() == Commo.TYPE.T1.code) {

                        // 请求分时数据
                        List<TrendData> tlist = QuotationCall.getInstance().getTrendDataOut(exchType, commo.getCode(), contract, "450");
                        redisService.put(CacheID.QUOTE_SLINE_PREFIX + commo.getId(), tlist);

                        // 请求日K数据
                        List<KlineData> kd1list = QuotationCall.getInstance().getDKlineData(exchType, commo.getCode(), contract, "30");
                        redisService.put(CacheID.QUOTE_KLINE_PREFIX + "D1:" + commo.getId(), kd1list);

                        // 请求3分K线数据
                        List<KlineData> km3list = QuotationCall.getInstance().getKlineData(exchType, commo.getCode(), contract, "3");
                        redisService.put(CacheID.QUOTE_KLINE_PREFIX + "M3:" + commo.getId(), km3list);

                        // 请求5分K线数据
                        List<KlineData> km5list = QuotationCall.getInstance().getKlineData(exchType, commo.getCode(), contract, "5");
                        redisService.put(CacheID.QUOTE_KLINE_PREFIX + "M5:" + commo.getId(), km5list);

                        // 请求15分K线数据
                        List<KlineData> km15list = QuotationCall.getInstance().getKlineData(exchType, commo.getCode(), contract, "15");
                        redisService.put(CacheID.QUOTE_KLINE_PREFIX + "M15:" + commo.getId(), km15list);

                        // 请求30分K线数据
                        List<KlineData> km30list = QuotationCall.getInstance().getKlineData(exchType, commo.getCode(), contract, "30");
                        redisService.put(CacheID.QUOTE_KLINE_PREFIX + "M30:" + commo.getId(), km30list);

                        // 请求60分K线数据
                        List<KlineData> km60list = QuotationCall.getInstance().getKlineData(exchType, commo.getCode(), contract, "60");
                        redisService.put(CacheID.QUOTE_KLINE_PREFIX + "M60:" + commo.getId(), km60list);

                    } else {

                        // 请求分时数据
                        List<TrendData> tlist = QuotationCall.getInstance().getTrendDataIn(commo.getCode(), contract);
                        System.out.println(commo.getCode()+"::"+tlist.size());
                        redisService.put(CacheID.QUOTE_SLINE_PREFIX + commo.getId(), tlist);

                        // 请求1分K线数据
                        List<KlineData> km1list = QuotationCall.getInstance().getKlineDataNative("0", commo.getCode(), contract, 1);
                        redisService.put(CacheID.QUOTE_KLINE_PREFIX + "M1:" + commo.getId(), km1list);

                        // 请求5分K线数据
                        List<KlineData> km5list = QuotationCall.getInstance().getKlineDataNative("0", commo.getCode(), contract, 2);
                        redisService.put(CacheID.QUOTE_KLINE_PREFIX + "M5:" + commo.getId(), km5list);

                        // 请求15分K线数据
                        List<KlineData> km15list = QuotationCall.getInstance().getKlineDataNative("0", commo.getCode(), contract, 3);
                        redisService.put(CacheID.QUOTE_KLINE_PREFIX + "M15:" + commo.getId(), km15list);

                        // 请求30分K线数据
                        List<KlineData> km30list = QuotationCall.getInstance().getKlineDataNative("0", commo.getCode(), contract, 4);
                        redisService.put(CacheID.QUOTE_KLINE_PREFIX + "M30:" + commo.getId(), km30list);

                        // 请求60分K线数据
                        List<KlineData> km60list = QuotationCall.getInstance().getKlineDataNative("0", commo.getCode(), contract, 5);
                        redisService.put(CacheID.QUOTE_KLINE_PREFIX + "M60:" + commo.getId(), km60list);

                        // 请求日K数据
                        List<KlineData> kd1list = QuotationCall.getInstance().getKlineDataNative("0", commo.getCode(), contract, 6);
                        redisService.put(CacheID.QUOTE_KLINE_PREFIX + "D1:" + commo.getId(), kd1list);

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


    /**
     * 国内期货设置主推代码
     */
    @Scheduled(cron = "0 55 8,20 * * ?")
    public void pushJob() throws InterruptedException {
        List<Commo> list = redisService.getList(CacheID.COMMO_LIST, Commo.class);
        if (CollectionUtils.isNotEmpty(list)) {
            for (Commo commo : list) {
                if (commo.getType().intValue() == Commo.TYPE.T2.code) {
                    try {
                        String contract = riskParamService.getContractNo(commo.getId());
                        QuotationCall.getInstance().setPushCode(commo.getCode() + contract);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

}
